package com.sky.com.sky.web.app;

import com.sky.dto.DishDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
public class DishController {

    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 菜品和菜品口味查询
     * @param categoryId
     * @return
     * **/
    @GetMapping("/list")
    public Result list(@RequestParam Long categoryId){
        //--------------------------------------
        //查询redis是否有缓存数据
        String key = "dish_" + categoryId;
        List<DishVO> dishes = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if(dishes != null && dishes.size() > 0){
            //缓存有，直接返回
            return Result.success(dishes);
        }
        //--------------------------------------
        //封装查询条件
        DishDTO dishDTO = DishDTO.builder()
                .categoryId(categoryId)
                .status(1)
                .build();
        //调用service查询
        dishes= dishService.listWithFlavor(dishDTO);
       //数据缓存到redis
        redisTemplate.opsForValue().set(key,dishDTO);
        return Result.success(dishes);
    }



}
